<template>
  <u-action-sheet
    :list="showGiveUp?actionGiveUpList: actionList"
    v-model="showActionSheet"
    @click="handleActionShellClick"
  ></u-action-sheet>
</template>

<script>
import loginOutMixin from '@/mixins/login-out.js';
import { cancelAuth } from '@/api/modules/company';

export default {
  mixins: [loginOutMixin],
  props: {
    show: Boolean,
    showGiveUp: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      personId: uni.getStorageSync("person-id").slice(2) || '',
      showActionSheet: false,
      actionList: [
        {
          text: "切换身份",
        },
        {
          text: "退出登录",
        },
      ],
      actionGiveUpList: [
        {
          text: "放弃认证",
        },
        {
          text: "切换身份",
        },
        {
          text: "退出登录",
        },
      ],
    };
  },
  watch: {
    show(newVal) {
      this.showActionSheet = newVal;
    },
    showActionSheet(newVal) {
      if (this.show != newVal) this.$emit("update:show", newVal);
    },
  },
  methods: {
    handleActionShellClick(index) {

      if(this.showGiveUp) {
        if (index == 0) {
          uni.showModal({
            title: '提示',
            content: '放弃企业认证后，将删除您之前的企业认证信息，您可重新进行认证，是否确认放弃？',
            success: (res) => {
              if (res.confirm) {
                this.giveUpCertication();
              }
            },
          });
        } else if (index == 1) {
          uni.navigateTo({
            url: "/pagesSub/public/character/index",
          });
        } else if (index == 2) {
          this.loginOut();
        }
      } else {
        if (index == 0) {
          uni.navigateTo({
            url: "/pagesSub/public/character/index",
          });
        } else if (index == 1) {
          this.loginOut();
        }
      }

    },
    giveUpCertication() {
      const params = {
        personId: this.personId,
      };
      uni.showLoading();
      cancelAuth(params).then(res => {
        uni.hideLoading();
        if(res && res.code == 200) {
          uni.showToast({
            title: '放弃成功',
            icon: 'none',
          });
          setTimeout(() => {
            this.toCompany();
          }, 1000);
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: 'none',
          });
        }
      }).catch(err => {
          uni.hideLoading();
      })
    },
    toPerson() {
      getApp().globalData.tabBarList = getApp().globalData.personTabs;
      getApp().globalData.loginStatus = 'person';
      uni.setStorageSync("login-status", 'person');
      getApp().globalData.msgUnreadCnt = 0;
      getApp().globalData.tim && getApp().globalData.tim.logout();
      
      uni.reLaunch({
        url: '/pages/userCenter/index',
      });
    },
    toCompany() {
      uni.reLaunch({
        url: '/pages/company/index',
      });
    },
  },
};
</script>

<style>
</style>